package com.zs.ktsms.controller;

import com.zs.ktsms.pojo.SysUser;
import com.zs.ktsms.service.SysUserService;
import com.zs.ktsms.utils.Page;
import com.zs.ktsms.utils.ResponseMessage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/31 下午9:50
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private SysUserService service;

    @GetMapping("/list")
    public ResponseMessage<Page<SysUser>> select(String realName,Long roleId,@RequestParam(name = "pageNo" ,defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        return ResponseMessage.success("用户列表加载成功",service.getPage(realName,roleId,pageNo,pageSize));
    }

    @PostMapping("/add")
    public ResponseMessage add(@RequestBody SysUser sysUser){
        int i = service.add(sysUser);
        if(i>0){
            return ResponseMessage.success("添加成功");
        }else{
            return ResponseMessage.error("添加失败");
        }
    }

    @GetMapping("/detail")
    public ResponseMessage detail(Integer id){
        return ResponseMessage.success("用户详情加载成功",service.detail(id));
    }

    @PostMapping("/update")
    public ResponseMessage update(@RequestBody SysUser sysUser){
        int i = service.update(sysUser);
        if(i>0){
            return ResponseMessage.success("修改成功");
        }else{
            return ResponseMessage.error("修改失败");
        }
    }

    @DeleteMapping("/del")
    public ResponseMessage delete(Integer id){
        int i = service.delete(id);
        if (i > 0) {
            return ResponseMessage.success("删除成功");
        }else{
            return ResponseMessage.error("删除失败");
        }
    }

    @PostMapping("/login")
    public ResponseMessage<SysUser> login(Map<String, String> map){
        String realName=map.get("realName");
        String password = map.get("password");
        SysUser user = service.select(realName,password);
        if(user==null){
            return ResponseMessage.error("用户名或密码错误");
        }
        return ResponseMessage.success("登录成功",user);
    }

}
